package com.airnewslinkosky.ticket.ui.client;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.CustomerInfo;
import com.airnewslinkosky.ticket.model.CustomerInfoResult;
import com.airnewslinkosky.ticket.net.WebAccessor;
import com.airnewslinkosky.ticket.pay.util.BaseHelper;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.airnewslinkosky.ticket.util.Constants;
import com.airnewslinkosky.ticket.util.FlyUtil;
import com.mobclick.android.MobclickAgent;
//新建机构客户
public class ClientAdd  extends BaseActivity implements OnClickListener{

	private ProgressDialog progressDialog = null;
	private EditText userName,agent,contact,telephone,mobile,email,fax,zip,address,remark;
	
	private SharedPreferences sp;
	private Toast toast;
	
	public Handler myHandler;
	public final int SUCCESS = 0;
	public final int USER_ERROR = 1;
	
	public static CustomerInfo customerInfo;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.client);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
        
		activityList.add(this);
		
		findViewById(R.id.backBut).setOnClickListener(this);
		findViewById(R.id.clientButton).setOnClickListener(this);
		rendData();
		setData();
		
		myHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case SUCCESS :
					toast = Toast.makeText(ClientAdd.this, "成功保存机构客户信息", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					
					Intent intentR = new Intent();
					intentR.setClass(ClientAdd.this, ClientList.class);
					ClientList.isSearch = true;
					startActivity(intentR);
					
					break;
				case USER_ERROR:
					BaseHelper.showDialog(ClientAdd.this, "提示", (String)msg.obj,R.drawable.infoicon);
					break;
				}
			}
		};
		
	}
	private void rendData() {
		userName = (EditText)findViewById(R.id.userName);
		agent = (EditText)findViewById(R.id.agent);
		contact = (EditText)findViewById(R.id.contact);
		telephone = (EditText)findViewById(R.id.telephone);
		mobile = (EditText)findViewById(R.id.mobile);
		email = (EditText)findViewById(R.id.email);
		fax = (EditText)findViewById(R.id.fax);
		zip = (EditText)findViewById(R.id.zip);
		address = (EditText)findViewById(R.id.address);
		remark =  (EditText)findViewById(R.id.remark);
	}
	private void setData() {
		if(customerInfo != null){
			userName.setText(customerInfo.CustomerName);
			agent.setText(customerInfo.SalesRepresent);
			contact.setText(customerInfo.Principal);
			telephone.setText(customerInfo.Telephone);
			mobile.setText(customerInfo.Mobile);
			email.setText(customerInfo.Email);
			fax.setText(customerInfo.Fax);
			zip.setText(customerInfo.Zip);
			address.setText(customerInfo.Address);
			remark.setText(customerInfo.Remark);
		}
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.clientButton:
			if ("".equals(userName.getText().toString()) || "".equals(agent.getText().toString()) || "".equals(contact.getText().toString()) || "".equals(mobile.getText().toString()) || "".equals(email.getText().toString())){
				BaseHelper.showDialog(ClientAdd.this, "提示", "请填写完整信息",R.drawable.infoicon);
				return;
			}
			if (!mobile.getText().toString().matches("[0-9]{11}")) {
				toast = Toast.makeText(ClientAdd.this, "手机号填写不正确", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				return;
			}
			if (!email.getText().toString().matches("\\w+@(\\w+\\.)+[a-z]{2,3}")) {
				toast = Toast.makeText(ClientAdd.this, "Email填写不正确", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				return;
			}
			
			
			String operateType = "INSERTGROUP";
			String customerNo = "";
			if(customerInfo != null){
				customerNo = customerInfo.CustomerNo;
				operateType = "UPDATEGROUP";
			}
			customerInfo = new CustomerInfo();
			customerInfo.CustomerNo = customerNo;
			customerInfo.CustomerName = String.valueOf(userName.getText());
			customerInfo.SalesRepresent = String.valueOf(agent.getText());
			customerInfo.Principal = String.valueOf(contact.getText());
			customerInfo.Telephone = String.valueOf(telephone.getText());
			customerInfo.Mobile = String.valueOf(mobile.getText());
			customerInfo.Fax = String.valueOf(fax.getText());
			customerInfo.Email = String.valueOf(email.getText());
			customerInfo.Address = String.valueOf(address.getText());
			customerInfo.Zip = String.valueOf(zip.getText());
			customerInfo.Remark = String.valueOf(remark.getText());;
			
			save(customerInfo,operateType);
			
			break;
		case R.id.backBut: 
			Intent intentR = new Intent();
			intentR.setClass(ClientAdd.this, ClientList.class);
			ClientList.isSearch = true;
			startActivity(intentR);
			break;
		}
	}

	private void save(final CustomerInfo customerInfo,final String operateType) {
		progressDialog = ProgressDialog.show(ClientAdd.this, "请稍等...", "保存机构客户可能会\n占用比较长时间...",true);
		final Thread thread = new Thread() {
			public void run() {
//				sp = getSharedPreferences("UserInfoB2B", Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
//		        String userIdLogin = sp.getString("userIdB2B", null);
//		        String userPwd =  sp.getString("userPwdB2B", null);
//		        String asToken =   sp.getString("ASTokenB2B", null);
		        String customerName = "";
		        String customerNo = "";
		        String phone = "";
		        
		        String userIdLogin = Constants.USERID;
		        String userPwd =  Constants.USERPWD;
		        String asToken =  Constants.ASToken;
		        
		        CustomerInfoResult customerInfoResult = WebAccessor.editCustomerInfoManage(ClientAdd.this,userIdLogin,userPwd,asToken,operateType,customerName,customerNo,phone,customerInfo);
		        
		        if(customerInfoResult.Result.equals("0")) {
		        	Message msg = new Message();
					msg.what = SUCCESS;
					myHandler.sendMessage(msg);
					progressDialog.dismiss();
					return;
		        } else {
		        	Message msg = new Message();
					msg.what = USER_ERROR;
					msg.obj = customerInfoResult.ErrorDescription;
					myHandler.sendMessage(msg);
					progressDialog.dismiss();
					return;
		        }
			}
		};
		thread.start();
	}
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
